﻿using System;
using System.Linq;
using System.Xml.Linq;

namespace Redmine.Laptop.Common.Settings.SettingsUtilities
{
    public static partial class CommonSettings
    {
        public static class RedmineSettings
        {
            private static readonly XElement RedmineSettingsElement;
            
            static RedmineSettings()
            {
                RedmineSettingsElement = Document.Descendants("RedmineSettings").Single();
            }

            public static string Host
            {
                get { return RedmineSettingsElement.Attribute("host").Value; }
            }

            public static bool IsUsedApiAuthentication
            {
                get { return Convert.ToBoolean(RedmineSettingsElement.Attribute("isUsedApiAuthentication").Value); }
            }

            public static string ApiKey
            {
                get { return RedmineSettingsElement.Element("Credentials").Attribute("apiKey").Value; }
                set 
                {
                    RedmineSettingsElement.Element("Credentials").Attribute("apiKey").SetValue(value);
                    Document.Save(XmlSettingsPath);
                }
            }

            public static string Login
            {
                get { return RedmineSettingsElement.Element("Credentials").Attribute("login").Value; }
                set
                {
                    RedmineSettingsElement.Element("Credentials").Attribute("login").SetValue(value);
                    Document.Save(XmlSettingsPath);
                }
            }

            public static string Password
            {
                get { return RedmineSettingsElement.Element("Credentials").Attribute("password").Value; }
                set
                {
                    RedmineSettingsElement.Element("Credentials").Attribute("password").SetValue(value);
                    Document.Save(XmlSettingsPath);
                }
            }
        }
    }
}
